<template>
  <HomePanel title="人气推荐" sub-title="人气爆款 不容错过" ref="target">
    <ul class="goods-list" v-if="list.length === 0">
      <li v-for="i in 4" :key="i">
      <XtxSkeleton :width="306" :height="306"/>
      </li>
    </ul>
    <ul class="goods-list" v-else>
      <li v-for="item in list" :key="item.id">
        <RouterLink to="/">
          <img :src="item.picture" alt="" />
          <p class="name">{{ item.title }}</p>
          <p class="desc">{{ item.alt }}</p>
        </RouterLink>
      </li>
    </ul>
  </HomePanel>
</template>

<script>
import HomePanel from './home-panel'
import { reqFindHot } from '@/api/home'
// 导入封装的懒加载函数
import { useObserver } from '@/compositions/index'
// import { ref } from 'vue'
// import { useIntersectionObserver } from '@vueuse/core'
export default {
  name: 'HomeHot',
  components: { HomePanel },
  setup () {
    const { list, target } = useObserver(reqFindHot)
    return { list, target }
    // const list = ref([])
    // const target = ref(null)
    // // console.log(target.value)
    // const { stop } = useIntersectionObserver(
    //   // 被检测的目标元素，可以是组件，也可以是DOM元素
    //   target,
    //   ([{ isIntersecting }], observerElement) => {
    //     // 如果元素可见为true 发送请求
    //     if (isIntersecting) {
    //       // 发送请求
    //       reqFindHot().then(({ result }) => {
    //         console.log(39, result)
    //         list.value = result
    //       })
    //       // 调用stop函数避免多次重复发送请求并且停止检测
    //       stop()
    //     }
    //   }
    // )
    // return { list, target }
  }
}
</script>

<style scoped lang='less'>
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 426px;
  li {
    width: 306px;
    height: 406px;
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
</style>
